package com.itheima.Utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.Map;

/**
 * jwt令牌工具类
 * 生成
 * 解析
 */
public class JwtUtils {
    //密钥  //过期时间
    private static String singKey = "6LaF5biC";
    private static long  expiryTime = 43200000L;

    /**
     * 生成令牌
     * @param claims
     * @return
     */
    public static String createJWT(Map<String, Object> claims) {
        String compact = Jwts.builder()
                .signWith(SignatureAlgorithm.HS256, singKey)
                .setExpiration(new Date(System.currentTimeMillis() + expiryTime))
                .addClaims(claims)
                .compact();
        return compact;
    }

    /**
     * 解析令牌
     * @param jwt
     * @return
     */
    public static Claims parseJWT(String jwt) {
        Claims claims = Jwts.parser()
                .setSigningKey(singKey)
                .parseClaimsJws(jwt)
                .getBody();
        return claims;
    }

}
